﻿/* 
 * 
 * Copyright 2013 Bendsoft. All rights reserved.
 * http://www.bendsoft.com
 * 
 * This code is distributed under the New BSD License (BSD).
 * 
 * @package     Camelot SharePoint Integration Plugins Library
 * @codeplex    http://camelottoolkit.codeplex.com
 * @license     New BSD License (BSD)
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Camelot.SharePointIntegration.IntegrationService;
using Camelot.SharePointIntegration.IntegrationService.Entities;
using Camelot.SharePointIntegration.Client;

namespace Camelot.SharePointIntegration.Plugins
{
    public class MySqlDumpRestore : ListDataPlugin
    {
        protected override ServicePluginResult ProcessData(Task task, ListData data, List<Parameter> parameters)
        {
            string database = (from p in parameters where p.Name.ToLower() == "database" select p.Value).FirstOrDefault();
            string mysqlbin = (from p in parameters where p.Name.ToLower() == "mysqlbin" select p.Value).FirstOrDefault();
            string hostname = (from p in parameters where p.Name.ToLower() == "hostname" select p.Value).FirstOrDefault();
            string username = (from p in parameters where p.Name.ToLower() == "username" select p.Value).FirstOrDefault();
            string password = (from p in parameters where p.Name.ToLower() == "password" select p.Value).FirstOrDefault();
            string encoding = (from p in parameters where p.Name.ToLower() == "encoding" select p.Value).FirstOrDefault();

            if (String.IsNullOrEmpty(mysqlbin))
                return new IntegrationService.ServicePluginResult() { Message = "Parameter null or empty: mysqlbin" };

            if (String.IsNullOrEmpty(hostname))
                return new IntegrationService.ServicePluginResult() { Message = "Parameter null or empty: hostname" };

            if (String.IsNullOrEmpty(username))
                return new IntegrationService.ServicePluginResult() { Message = "Parameter null or empty: username" };

            if (String.IsNullOrEmpty(password))
                return new IntegrationService.ServicePluginResult() { Message = "Parameter null or empty: password" };

            if (String.IsNullOrEmpty(encoding))
                encoding = "ISO-8859-1";

            if (!mysqlbin.EndsWith("\\"))
                mysqlbin += "\\";

            if (!File.Exists(mysqlbin + "mysql.exe"))
                return new IntegrationService.ServicePluginResult() { Message = "The mysql.exe executable does not exist" };

            var sql = Camelot.SharePointIntegration.MySql.CreateMySqlDump(database, data);
            var result = Utilities.ExecuteProcess(mysqlbin + "mysql.exe", "--host=" + hostname + " --user=" + username + " --password=" + password, 20000, Encoding.GetEncoding(encoding).GetBytes(sql));

            if (!string.IsNullOrEmpty(result))
                return new IntegrationService.ServicePluginResult() { Success = false, Message = result };
            else
                return new IntegrationService.ServicePluginResult() { Success = true };
        }
    }
}
